<?php
declare (strict_types=1);

namespace app\controller\api;

use think\Exception;
use think\facade\App;
use think\Response;
use PHPExcel_IOFactory;
use PHPExcel;
use app\model\AdminBudget;

class Miss extends Base {

    public function index(): Response {
        $version = config('apiadmin.APP_VERSION');
        if (!$version) {
            throw new Exception('请先执行安装脚本，完成项目初始化！');
        } else {
            return $this->buildSuccess([
                'Product'    => config('apiadmin.APP_NAME'),
                'ApiVersion' => $version,
                'TpVersion'  => App::version(),
                'Company'    => config('apiadmin.COMPANY_NAME'),
                'ToYou'      => "1111111111111I'm glad to meet you（终于等到你！）"
            ]);
        }
    }
    public function test():Response{
        $file_name = root_path() . 'public/uploads/2222.xlsx';   //上传文件的地址
        $name=strrchr('1111.xlsx','.');
        if($name=='.xlsx'){
            $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
        }elseif($name=='.xls'){
            $objReader =\PHPExcel_IOFactory::createReader('Excel5');
        }elseif($name=='.csv'){
            $objReader =\PHPExcel_IOFactory::createReader('csv');
        }
        $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

        $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
        // array_shift($excel_array);  //删除第一个数组(标题);
    //    var_dump($excel_array);exit;
        foreach ($excel_array as $k=>$v){
            $insert = [
                'district_code'=>$v[1],
                'district_name'=>$v[2],
                'institutions_code'=>$v[3],
                'institutions_name'=>$v[4],
                'departments_code'=>$v[5],
                'departments_name'=>$v[6],
                'project_category_code'=>$v[7],
                'project_category_name'=>$v[8],
                'project_code'=>$v[9],
                'project_name'=>$v[10],
                'functional_category_code'=>$v[11],
                'functional_category_name'=>$v[12],
                'functional_category_code_class'=>$v[13],
                'functional_category_name_class'=>$v[14],
                'functional_category_code_fund'=>$v[15],
                'functional_category_name_fund'=>$v[16],
                'functional_category_code_item'=>$v[17],
                'functional_category_name_item'=>$v[18],
                'economics_category_code'=>$v[19],
                'economics_category_name'=>$v[20],
                'economics_category_code_class'=>$v[21],
                'economics_category_name_class'=>$v[22],
                'economics_category_code_fund'=>$v[23],
                'economics_category_name_fund'=>$v[24],
                'total'=>$v[25],
                'fiscal_appropriation'=>$v[26],
                'general_public_budget'=>$v[27],
                'government_budgets'=>$v[28],
                'government_capital_management_budget'=>$v[29],
                'special_financial_account'=>$v[30],
                'utility_income'=>$v[31],
                'institution_management_income'=>$v[32],
                'institution_other_income'=>$v[33],
                'type'=>$v[34],
            ];
            $insertData[] = $insert;
        }
        (new AdminBudget())->insertAll($insertData);

        return $this->buildSuccess();
    }
}
